package com.qf.mybatis01.jdbc;

import com.qf.mybatis01.entity.User;

import java.sql.*;

public class JdbcDemo {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1.加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false";
        //2.创建connection对象
        Connection connection = DriverManager.getConnection(url, "root", "root");
        //3.创建prestatement对象
        String sql  = "select * from t_users where id=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //给限定符赋值
        preparedStatement.setInt(1,1);
        //4.执行 sql
        ResultSet rs = preparedStatement.executeQuery();

        //5.遍历结果集
        while (rs.next()) {
            //手工ORM
            User user = new User();
            user.setId(rs.getInt("id"));
            user.setName(rs.getString("name"));
            user.setSex(rs.getString("sex"));
            System.out.println(user);
        }

        //6.关闭资源
        rs.close();
        preparedStatement.close();
        connection.close();


    }
}
